package classesBD;

import aplicacao.funcoes;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pessoal
 */
public class Congresso extends MeioPublicacao{
  private int idCongresso;
  private String url;
  private int serie;
  private String isbn;
  private String ee;
  private String publisher;
  private String crossRef;
  private String tituloLivro;
  private String series;
  private Journal journal;
  private ArrayList<String> editores;

  public Congresso() {
    editores = new ArrayList();
    
  }

  public String getCrossRef() {
    return crossRef;
  }
  
  
  
  
  @Override
  public int insere(boolean checaExistencia){
    String[] campos;
    String query;
    if (checaExistencia){
      idCongresso= existeCrossRef(crossRef);
    }
    if (idCongresso == 0){
        try {
            campos = new String[11]; 
            campos[0] = "default";
            if(super.nome ==  null){
              super.nome = crossRef;
            }
            campos[1] = Integer.toString(super.insere(true));
            campos[2] = url;
            campos[3] = Integer.toString(serie);
            campos[4] = isbn;
            campos[5] = ee;
            campos[6] = publisher;
            campos[7] = crossRef;
            campos[8] = tituloLivro;
            campos[9] = series;
            if (journal != null){
              campos[10] = Integer.toString(journal.getIdJournal());
            }  
            query = funcoes.montaInsert("CONGRESSO", campos);
            idCongresso =  Conexao.executa(query, true);
            Conexao.fecha();
        } catch (SQLException ex) {
            Logger.getLogger(Congresso.class.getName()).log(Level.SEVERE, null, ex);
        }
    }else{
      atualiza();
    }
    if (idCongresso != 0){
      campos = new String[2];
      for(int i=0;i<editores.size();i++){
          try {
              campos[0] = Integer.toString(idCongresso);
              Pessoa p = new Pessoa(editores.get(i));
              campos[1] = Integer.toString(p.insere(true));
              query = funcoes.montaInsert("EDITORACAO", campos);
              Conexao.executa(query);
              Conexao.fecha();
          } catch (SQLException ex) {
              Logger.getLogger(Congresso.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
    }
    return idCongresso;  
  }

  @Override
  public void buscaPorId(int id) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  @Override
  public void buscaPorNome(String nome) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  @Override
  public boolean preencheAttr(String attr, String valor) {
    String atributo = attr.toLowerCase();
    boolean result = true;
    switch(atributo){
      case "url":url = valor;
        break;
      case "series":series = valor;
        break;
      case "serie":serie = Integer.parseInt(valor);
        break;
      case "isbn": isbn = valor;
        break;
      case "ee": ee = valor;
        break;
      case "publisher":publisher = valor;
        break;
      case "key":  
      case "crossref":crossRef = valor;
        break;
      case "booktitle":tituloLivro = valor;
        break;
      case "editor":editores.add(valor);
        break;
      case "journal":editores.add(valor);
        break;  
      default: result = super.preencheAttr(attr, valor);
    } 
    return result;
  }
  
  @Override
  public String existe(int id) {
    String query = "SELECT CROSSREF FROM CONGRESSO WHERE IDCONGRESSO=" + Integer.toString(id);
    String[][] retorno = Conexao.pesquisa(query, 1);
    if (retorno.length > 0){
      return retorno[0][0];
    }else{
      return "";
    }
  }

  public int existeCrossRef(String cRef){
    if (cRef == null){
      return 0;
    }
    cRef = cRef.toLowerCase();
    String query = "SELECT IDCONGRESSO FROM CONGRESSO WHERE LOWER(CROSSREF)=" + funcoes.quotedStr(cRef);
    String[][] retorno = Conexao.pesquisa(query, 1);
    if (retorno.length > 0){
      return Integer.parseInt(retorno[0][0]);
    }else{
      return 0;
    }
  }
  
  public void atualiza(){
    String id = Conexao.pesquisa("SELECT IDMEIOP FROM CONGRESSO WHERE IDCONGRESSO = " + idCongresso,1)[0][0];
    if (!funcoes.isNullOrEmpty(id)){
      super.atualiza(Integer.parseInt(id));
    }  
    
    String query = "UPDATE CONGRESSO SET ";
    if(!funcoes.isNullOrEmpty(url)){
      query += " url=" + funcoes.quotedStr(url) + ",";
    }
    if(serie != 0){
      query += " serie=" + serie + ",";
    }
    if(!funcoes.isNullOrEmpty(isbn)){
      query += " isbn="+funcoes.quotedStr(isbn) + ",";
    }
    if(!funcoes.isNullOrEmpty(ee)){
      query += " ee="+funcoes.quotedStr(ee) + ",";
    }
    if(!funcoes.isNullOrEmpty(publisher)){
      query += " publisher="+funcoes.quotedStr(publisher) + ",";
    }
    if(!funcoes.isNullOrEmpty(tituloLivro)){
      query += " tituloLivro="+funcoes.quotedStr(tituloLivro) + ",";
    }
    if(!funcoes.isNullOrEmpty(series)){
      query += " series="+funcoes.quotedStr(series) + ",";
    }  
    if(journal != null){
      query += " idJournal="+journal.getIdJournal();
    }
    if(query.endsWith(",")){
      query = query.substring(0, query.length()-1);
    }
    query += " WHERE IDCONGRESSO = "+idCongresso;
    if (query.contains(",")){
        try {
            Conexao.executa(query);
            Conexao.fecha();
        } catch (SQLException ex) {
            Logger.getLogger(Congresso.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  
  }
}
